import requests,re

class Novel:
    def __init__(self,catalog_url,
                 regular_book_con = '<td class="L"><a href="(.*?)">(.*?)</a></td>',
                 regular_book_name = '<h1>(.*?)</h1>',
                 regular_book_author = '<h3>.*?作者：(.*?)&nbsp;&nbsp;更新时间：.*?</h3>',
                 regular_book_info = '<dd id="contents">(.*?)</dd>'
                 ):
        self.catalog_url = catalog_url
        #self.novel_con = self.Novel_con()
        self.regular_book_con = regular_book_con
        self.regular_book_name = regular_book_name
        self.regular_book_author = regular_book_author
        self.regular_book_info = regular_book_info
    def Novel_con(self):
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36'}
        res = requests.get(self.catalog_url,headers = headers)
        res.encoding = 'gbk'
        html = res.text
        #print(html)
        re_book_name = re.findall(self.regular_book_name,html)[0].replace(' 最新章节更新列表','')
        #print(re_book_name)
        re_book_autor = re.findall(self.regular_book_author,html)[0]
        info_list = re.findall(self.regular_book_con,html)
        #print(re_book_name,re_book_autor,info_list)
        return info_list,re_book_name,re_book_autor
    def save_paper(self):
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36'}
        book_list = self.Novel_con()[0]
        #print(book_list)
        book_name = self.Novel_con()[1]
        book_author = self.Novel_con()[2]
        for i in book_list[:10]:
            capter_url = self.catalog_url+i[0]
            #print(capter_url)
            capter_html = requests.get(capter_url,headers = headers)
            capter_html.encoding = 'gbk'
            capter_html = capter_html.text
            book_content = re.search(self.regular_book_info,capter_html,re.S)
            book_content = book_content.group(1).replace('&nbsp;',' ').replace('<br />','\n')
            txt_name = '{}--{}.txt'.format(book_name,book_author)
            with open(txt_name,'a') as f:
                f.write(i[1])
                f.write('\n\n')
                f.write(book_content)
                f.write('\n\n')
        return txt_name
if __name__ == '__main__':
    pass
    '''
    url = 'https://www.x23us.com/html/67/67117/'
    x = Novel(catalog_url = url)
    x.save_paper()
    '''
